# -*- coding:utf-8 -*-
# created on 2017/4/30
# 

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex
from sympy import cos


# 正方形建系
class XLBuildSquareAxis(BaseFunction):
    def solver(self, *args):
        name = args[0]
        square = self.search(name)
        point_a_name = square.point_A_name
        point_b_name = square.point_B_name
        point_c_name = square.point_C_name
        point_d_name = square.point_D_name
        line_ab_name = square.line_AB_name
        line_da_name = square.line_DA_name
        edge_length = square.edge_length
        v_eqs = square.Eqs
        if not edge_length:
            edge_length = sympify('a')
            square.line_AB_value = edge_length
            square.line_BC_value = edge_length
            square.line_CD_value = edge_length
            square.line_DA_value = edge_length
            self.steps.append(["", "设正方形的边长为%s, 得" % new_latex(edge_length)])
            v_eqs.append([edge_length, ">", S.Zero])
        self.steps.append(["", "以%s为坐标原点,%s所在直线为x轴,%s所在直线为y轴建立坐标系,得"
                           % (new_latex(point_a_name), new_latex(line_ab_name), new_latex(line_da_name))])
        point_a_axis = BasePoint({"name": point_a_name, "value": [0, 0]})
        point_b_axis = BasePoint({"name": point_b_name, "value": [edge_length, 0]})
        point_c_axis = BasePoint({"name": point_c_name, "value": [edge_length, edge_length]})
        point_d_axis = BasePoint({"name": point_d_name, "value": [0, edge_length]})
        self.steps.append(["", "∴ %s, %s, %s, %s" % (point_a_axis.printing(), point_b_axis.printing(),
                                                     point_c_axis.printing(), point_d_axis.printing())])
        square.point_A_Axis = point_a_axis
        square.point_B_Axis = point_b_axis
        square.point_C_Axis = point_c_axis
        square.point_D_Axis = point_d_axis
        self.label.add("建系-正方形")
        self.output.append(square)
        return self


# 菱形建系
class XLBuildDiamondAxis(BaseFunction):
    def solver(self, *args):
        name = args[0]
        diamond = self.search(name)
        point_a_name = diamond.point_A_name
        point_b_name = diamond.point_B_name
        point_c_name = diamond.point_C_name
        point_d_name = diamond.point_D_name
        edge_length = diamond.edge_length
        v_eqs = diamond.Eqs
        if not edge_length:
            edge_length = sympify('a')
            diamond.line_AB_value = edge_length
            diamond.line_BC_value = edge_length
            diamond.line_CD_value = edge_length
            diamond.line_DA_value = edge_length
            self.steps.append(["", "设菱形的边长为%s, 得" % new_latex(edge_length)])
            v_eqs.append([edge_length, ">", S.Zero])
        angle_a_value = diamond.Angle_A_value
        angle_b_value = diamond.Angle_B_value
        angle_c_value = diamond.Angle_C_value
        angle_d_value = diamond.Angle_D_value
        if (not angle_a_value) and (not angle_b_value) and (not angle_c_value) and (not angle_d_value):
            diamond.angle_init()
            angle_a_value = diamond.Angle_A_value
            angle_b_value = diamond.Angle_B_value
            angle_c_value = diamond.Angle_C_value
            angle_d_value = diamond.Angle_D_value
            self.steps.append(["", "设∠%s = %s" % (new_latex(point_a_name), new_latex(angle_a_value))])
        else:
            diamond.angle_init()
            angle_a_value = diamond.Angle_A_value
            angle_b_value = diamond.Angle_B_value
            angle_c_value = diamond.Angle_C_value
            angle_d_value = diamond.Angle_D_value
        self.steps.append(["", "以%s, %s所在直线为x轴, %s, %s所在直线为y轴建立坐标系, 则"
                           % (new_latex(point_a_name), new_latex(point_c_name), new_latex(point_b_name),
                              new_latex(point_d_name))])
        point_a_axis = BasePoint({"name": point_a_name, "value": [- edge_length * cos(angle_a_value / 2), 0]})
        point_b_axis = BasePoint({"name": point_b_name, "value": [0, - edge_length * cos(angle_b_value / 2)]})
        point_c_axis = BasePoint({"name": point_c_name, "value": [edge_length * cos(angle_c_value / 2), 0]})
        point_d_axis = BasePoint({"name": point_d_name, "value": [0, edge_length * cos(angle_d_value / 2)]})
        self.steps.append(["", "∴ %s, %s, %s, %s" % (point_a_axis.printing(), point_b_axis.printing(),
                                                     point_c_axis.printing(), point_d_axis.printing())])
        diamond.point_A_Axis = point_a_axis
        diamond.point_B_Axis = point_b_axis
        diamond.point_C_Axis = point_c_axis
        diamond.point_D_Axis = point_d_axis
        self.label.add("建系-菱形")
        self.output.append(diamond)
        return self


# 矩形建系
class XLBuildRectangleAxis(BaseFunction):
    def solver(self, *args):
        name = args[0]
        rectangle = self.search(name)
        point_a_name = rectangle.point_A_name
        point_b_name = rectangle.point_B_name
        point_c_name = rectangle.point_C_name
        point_d_name = rectangle.point_D_name
        line_ab_name = rectangle.line_AB_name
        line_bc_name = rectangle.line_BC_name
        line_da_name = rectangle.line_DA_name
        v_eqs = rectangle.Eqs
        line_ab_value = sympify('a')
        line_bc_value = sympify('b')
        v_eqs.append([line_ab_value, ">", S.Zero])
        v_eqs.append([line_bc_value, ">", S.Zero])
        rectangle.line_AB_value = line_ab_value
        rectangle.line_BC_value = line_bc_value
        rectangle.line_CD_value = line_ab_value
        rectangle.line_DA_value = line_bc_value
        self.steps.append(["", "设%s = %s, %s = %s, 则"
                           % (new_latex(line_ab_name), new_latex(line_ab_value), new_latex(line_bc_name),
                              new_latex(line_bc_value))])
        self.steps.append(["", "以%s为坐标原点, %s所在直线为x轴, %s所在直线为y轴建立坐标系"
                           % (new_latex(point_a_name), new_latex(line_ab_name), new_latex(line_da_name))])
        point_a_axis = BasePoint({"name": point_a_name, "value": [0, 0]})
        point_b_axis = BasePoint({"name": point_b_name, "value": [line_ab_value, 0]})
        point_c_axis = BasePoint({"name": point_c_name, "value": [line_ab_value, line_bc_value]})
        point_d_axis = BasePoint({"name": point_d_name, "value": [0, line_bc_value]})
        self.steps.append(["", "∴ %s, %s, %s, %s" % (point_a_axis.printing(), point_b_axis.printing(),
                                                     point_c_axis.printing(), point_d_axis.printing())])
        rectangle.point_A_Axis = point_a_axis
        rectangle.point_B_Axis = point_b_axis
        rectangle.point_C_Axis = point_c_axis
        rectangle.point_D_Axis = point_d_axis
        self.label.add("建系-矩形类")
        self.output.append(rectangle)
        return self
